package com.yuanli.cosmeticmirror.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.BaseActivity;
import com.yuanli.cosmeticmirror.model.iview.IFindPwdView;
import com.yuanli.cosmeticmirror.persenter.FindPwdPersenter;
import com.yuanli.cosmeticmirror.utils.GeneralUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FindPwdActivity extends BaseActivity implements IFindPwdView {
    @BindView(R.id.find_account)
    EditText findAccount;
    @BindView(R.id.find_code)
    EditText findCode;
    @BindView(R.id.find_get_code)
    TextView findGetCode;
    @BindView(R.id.find_password)
    EditText findPassword;
    @BindView(R.id.find_enter_password)
    EditText findEnterPassword;
    @BindView(R.id.find_enter)
    TextView findEnter;
    @BindView(R.id.find_to_login)
    TextView findToLogin;

    private FindPwdPersenter persenter;
    private long mLastClickTime = 0;
    public final long TIME_INTERVAL = 1000L;
    private static int tempSecond = 60; // 重新发送短信时间
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    findGetCode.setText("重新发送(" + tempSecond-- + "s)");
                    if (tempSecond > 0) {
                        handler.sendEmptyMessageDelayed(1, 1000); // 重新发送ns秒
                    } else {
                        handler.sendEmptyMessage(2); // 重新获取验证码
                    }
                    break;
                case 2:
                    findGetCode.setText("获取验证码");
                    //      mBtnGetCode.setTextColor(getResources().getColor(R.color.c_222));
                    findGetCode.setClickable(true);
                    tempSecond = 60;
                    break;
                case 100:
                    break;
            }
        }
    };

    @Override
    protected int getContentView() {
        return R.layout.activity_find;
    }

    @Override
    protected void init(Bundle bundle) {
        persenter = new FindPwdPersenter(this, this);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void destroy() {
        handler.removeCallbacksAndMessages(null);
        handler = null;
    }


    @OnClick({R.id.find_get_code, R.id.find_enter, R.id.find_to_login})
    public void onViewClicked(View view) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - mLastClickTime < TIME_INTERVAL) {
            // do something
            return;
        }
        mLastClickTime = nowTime;
        switch (view.getId()) {
            case R.id.find_get_code:
                String phone = findAccount.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    showToast("请输入手机号");
                    return;
                }
                if (GeneralUtils.isNotTel(phone)) {
                    showToast("请输入正确的手机号");
                    return;
                }
                tempSecond = 60;
                handler.sendEmptyMessage(1); // 重新发送ns秒
                findGetCode.setClickable(false); // 取消点击事件1
                persenter.getCode(phone);
                break;
            case R.id.find_enter:
                String phone1 = findAccount.getText().toString();
                String password = findPassword.getText().toString();
                String code = findCode.getText().toString();
                String enterPassword = findEnterPassword.getText().toString();

                if (TextUtils.isEmpty(code)) {
                    showToast("请先获取验证码");
                    return;
                }
                if (TextUtils.isEmpty(phone1)) {
                    showToast("请输入手机号");
                    return;
                }
                if (GeneralUtils.isNotTel(phone1)) {
                    showToast("请输入正确的手机号");
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    showToast("请输入新密码");
                    return;
                }
                if (TextUtils.isEmpty(enterPassword)) {
                    showToast("请重复输入新密码");
                    return;
                }
                if (!TextUtils.equals(password, enterPassword)) {
                    showToast("两次密码不一致，请重新输入");
                    return;
                }
                showLoadDialog();
                persenter.findPwd(phone1, password);
                break;
            case R.id.find_to_login:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onSucess() {
        dialogDismiss();
        showToast("修改成功，请用新密码进行登录");
        startActivity(new Intent(this, LoginActivity.class).putExtra("userName", !TextUtils.isEmpty(findAccount.getText().toString()) ? findAccount.getText().toString() : ""));
        finish();
    }

    @Override
    public void onError(String msg) {
        dialogDismiss();
        showToast(msg);
    }

    @Override
    public void getCodeSuncess(String object) {
        //        showToast(object);
        showToast("获取验证码成功");
        findCode.setFocusable(true);
        findCode.setFocusableInTouchMode(true);
    }
}
